import time

from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.common.by import By

from selenium import webdriver
from selenium.common import NoSuchElementException
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=options)

from Pages import BasePage


class PlanePage(BasePage.Action):

    def __init__(self):
        self.close_xpath = (By.XPATH,"//div[@class = 'close']/span")
        #出发日期输入框
        # self.inputGoDate_xpath = (By.XPATH,"//div[@class = 'datepicker depart']//input")

        #出发日期 = 当前日期+28天
        self.datePlaneGoDay = time.strftime('%Y-%m-%d', time.localtime(time.time()+8640000))
        # self.datePlaneGoDay_xpath1 = (By.XPATH,"//input[@class='form-control datepicker-input  input-hasicon no-empty' and @data-date='%s']" % self.datePlaneGoDay)
        # self.datePlaneGoDay_xpath = (By.XPATH,"//span[contains(@class,'day-cell') and not(contains(@class,'datepicker-item-disabled')) and @data-date='%s']" % self.datePlaneGoDay)
        self.dateIntFlightGoDay_xpath = (By.XPATH,"//div[@class='iflight-search']//span[contains(@class,'day-cell') and not(contains(@class,'datepicker-item-disabled')) and @data-date='%s']/div" % self.datePlaneGoDay)
        #定位出发日期
        self.godata_xpath = (By.XPATH, '//input[@class="form-control datepicker-input  input-hasicon no-empty"]')
        #修改出发日期
        self.get_xpath = (By.XPATH,'//input[@class="form-control datepicker-input  input-hasicon no-empty"])[1]')
        #LL修改出发城市
        self.gotoCity_xpath = (By.XPATH, "(//input[@placeholder = '请选择出发地'])[1]")
        #出发城市、到达城市
        self.aimCity_xpath = (By.XPATH,"(//input[@placeholder = '请选择目的地'])[1]")
        #GHJKL
        self.GHJKLCity_xpath = (By.XPATH,"(//div[@class='flight-search']//li[contains(text(),'GHJKL')])[1]")
        #LL修改GHJKL定位
        self.GHJKLCity_xpath1 = (By.XPATH, "(//div[@class='flight-search']//li[contains(text(),'GHJKL')])[2]")
        #杭州
        self.hangZhou_xpath = (By.XPATH,"(//div[@class='flight-search']//span[@title = '杭州'])[1]")
        #广州
        self.guangZhou_xpath = (By.XPATH,"//div[@class='flight-search']//span[@title = '广州']")
        #ll修改广州
        self.guangZhou_xpath1 = (By.XPATH,"//div[@class='tab-con active']//span[@title='广州']")
        #国际机票
        self.intFlight_xpath = (By.XPATH, "//li[contains(text(),'国际/中国港澳台')]")
        #国际单程
        self.oneWay_xpath = (By.XPATH, "//div[@class='iflight-search']//label[@role='radio']/span[text()='单程']")
        #国际城市-亚洲
        self.Asia_xpath = (By.XPATH, "//div[@class='iflight-search']//li[contains(text(),'亚洲')]")
        #曼谷
        self.Bangkok_xpath = (By.XPATH, "//div[@class='iflight-search']//div[@class='tab-con active']/ul/li[@title = '曼谷']")
        #上海浦东
        #self.PVG_xpath = (By.XPATH, "//div[@class='iflight-search']//li[contains(text(),'上海浦东')]")
        self.PVG_xpath = (By.XPATH, '//*[@id="app"]/div/div[1]/div[1]/div[1]/div/div/div[2]/div[2]/div/div/div[2]/div[1]/div[1]/div/div[2]/div[3]/div/div/div[1]/ul/li[2]')

        #国际 预定
        self.intFlight_yuding = "//div[@class = 'flight_list']//div[contains(text(),'预订')]]"
        #国际 预定按钮
        self.intFlight_yuding_anniu = (By.CSS_SELECTOR,".flight_list_btn2")
        #self.intFlight_yuding_anniu = (By.XPATH,"//div[class='flight_list_btn2']")
        #国际 预定
        # self.intFlight_reBuyPlane = "//div[@class = 'flight_list']/div[%d]/div/div[4]"
        self.intFlight_reBuyPlane = "//div[@class = 'flight_list']/div[%d]/div/div[2]/div[5]"



        #重要通告弹框关闭
        self.closeNotice_xpath = (By.XPATH,"//div[text() = '重要公告']/../../div/span")
        #往返
        self.wangfan_xpath = (By.XPATH,"//span[text() = '往返']/../span[1]")
        #机票搜索
        self.seaerBtn_xpath = (By.XPATH,"//button[text() = '搜索']")
        #预定
        self.reBuyPlane = "//div[@class = 'flight_list']/div[%d]//*[contains(text(),'预订')]"
        #预定去程
        self.reBugGoTicket_loc1 = (By.CSS_SELECTOR,".flight_list_btn3")
        self.reBugGoTicket_loc = (By.XPATH,"(//div[@class = 'flight_list_btn3'])[1]")
        #不选择推荐航班，继续预订
        self.continueBook_xpath = (By.XPATH,"//span[text()='不选择推荐航班，继续预订']")
        #提交按钮
        self.submitPlane_xpath = (By.XPATH,"//a[text() ='提交机票订单']")
        #
        self.need_loc = (By.CSS_SELECTOR,".el-button--small")
        #提交行程
        self.submitxingcheng_xpath = (By.XPATH,"//button[text() = '提交行程']")
        #违规原因
        self.chooseReason_xpath = (By.XPATH,"//input[@placeholder='请选择原因']")
        #违规原因列表
        self.violationReasons_loc = (By.CSS_SELECTOR, ".el-select-dropdown__item")
        #违规原因备注
        self.violationRemark_loc = (By.CSS_SELECTOR, ".violation-textarea textarea")
        #确认购保
        self.insuranceCheck_loc = (By.CSS_SELECTOR, ".nextStep")
        #保险条款确认
        self.infornmationCheck_loc = (By.XPATH, "//span[text()='我已阅读且同意']")
        #行程提交项，待补充，先配置为空

        #姓名证件确认
        self.checkName_loc = (By.XPATH, "//div[text()='核对无误，确认提交']")

        #订单中心
        self.orderList_xpath = (By.XPATH,"//li[contains(text(),'订单中心')]")


    # def submitTrip(self):
    #     self.smart_click(*self.submitxingcheng_xpath)
    #     time.sleep(2)

    def chooseViolationReason(self):
        if self.find_elements(*self.chooseReason_xpath):
            self.smart_click(*self.chooseReason_xpath)
            time.sleep(2)
            self.js_click(self.find_elements(*self.violationReasons_loc)[-1])
            time.sleep(1)
            self.find_elements(*self.violationRemark_loc)[0].send_keys("111remark")
            time.sleep(1)


    def switchToIntFlight(self):
        self.find_element(*self.intFlight_xpath).click()
        time.sleep(1)

    def click_submitPlane(self):
        self.chooseViolationReason()
        self.find_element(*self.submitPlane_xpath).click()
        time.sleep(3)
        #相似单继续下单
        if self.find_elements(*self.need_loc):
            self.js_click(self.find_elements(*self.need_loc)[1])
            time.sleep(3)

        #确认姓名证件
        if self.find_element(*self.checkName_loc):
            self.find_element(*self.checkName_loc).click()
            time.sleep(2)

        #确认购保
        if self.find_elements(*self.insuranceCheck_loc):
            self.find_element(*self.infornmationCheck_loc).click()
            time.sleep(1)
            self.js_click(self.find_elements(*self.insuranceCheck_loc)[0])
            time.sleep(5)



    #跳过保险
    def skipInsurance(self):
        pass




    def reBugGoTicket(self,index):
        time.sleep(1)
        self.find_elements(*self.reBugGoTicket_loc)[index].click()

    #机票订购顺序
    def reBuyPlaneTiket(self,index=1): # index:第几行
        sleepTime = 10
        planTicket_xpath = (By.XPATH,self.reBuyPlane % index)
    #    print(planTicket_xpath)
        time.sleep(sleepTime)
        self.smart_click(*planTicket_xpath)
        self.reBugGoTicket(0)
        time.sleep(sleepTime)
    #    self.refresh_window()
    #    time.sleep(sleepTime)
    #    self.smart_click(*planTicket_xpath,isWait=True)
    #    time.sleep(sleepTime)
    #    self.reBugGoTicket(1)


    def intFlight_reBugGoTicket(self):
        self.find_elements(*self.intFlight_yuding_anniu)[0].click()


    #国际机票订购顺序
    def intFlight_reBuyPlaneTiket(self,index=2): # index:第几行
        sleepTime = 15
        planTicket_xpath = (By.XPATH,self.intFlight_reBuyPlane % index)
    #    print(planTicket_xpath)
        time.sleep(sleepTime)
        self.smart_click(*planTicket_xpath)
        time.sleep(0.5)
        self.intFlight_reBugGoTicket()
        time.sleep(sleepTime)

    def click_searchBtn(self,index):
        ele = self.find_elements(*self.seaerBtn_xpath)[index]
        self.js_click(ele)

    def click_wangfan(self):
        try:
            self.find_elements(*self.wangfan_xpath)[0].click()
        except:
            pass
    #弹框判断
    def closeNotice(self):
        time.sleep(5)
        if self.Element_displayed(*self.closeNotice_xpath):
            self.smart_click(*self.closeNotice_xpath)

    def click_Close(self):
        self.smart_click(*self.close_xpath)
        time.sleep(0.5)

    #国内机票
    def select_PlaneGoDate(self):
        # self.find_elements(*self.inputGoDate_xpath)[0].click()
        # time.sleep(5)
        # self.find_elements(*self.datePlaneGoDay_xpath1)[0].click()
        time.sleep(2)
        js = 'document.getElementsByClassName("form-control datepicker-input input-hasicon no-empty")[0].removeAttribute("readonly");'
        self._driver.execute_script(js)
        self.find_element(By.XPATH, '//input[@class="form-control datepicker-input  input-hasicon no-empty"]').clear()
        self.find_element(By.XPATH,'(//input[@class="form-control datepicker-input  input-hasicon no-empty"])[1]').send_keys('2026-02-10')
        ActionChains(driver).send_keys(Keys.ENTER).perform()
        time.sleep(5)
    def select_PlaneFromCity(self):
        # self.find_elements(*self.aimCity_xpath)[0].click()
        time.sleep(2)
        self.find_elements(*self.gotoCity_xpath)[0].click()
        time.sleep(2)
        self.find_elements(*self.GHJKLCity_xpath)[0].click()
        time.sleep(2)
        self.find_elements(*self.hangZhou_xpath)[0].click()
        time.sleep(2)

    def select_PlaneToCity(self):
        self.find_elements(*self.aimCity_xpath)[0].click()
        time.sleep(2)
        self.find_elements(*self.GHJKLCity_xpath1)[0].click()
        time.sleep(5)
        self.find_elements(*self.guangZhou_xpath1)[0].click()
        time.sleep(5)

    #国际机票
    def select_intFlightFromCity(self):
        self.find_elements(*self.aimCity_xpath)[0].click()
        # self.find_elements(*self.gotoCity_xpath)[0].click()
        #self.find_elements(*self.Asia_xpath)[0].click()
        time.sleep(3)
        self.find_elements(*self.PVG_xpath).click()
        time.sleep(3)

    def select_intFlightToCity(self):
        self.find_elements(*self.aimCity_xpath)[1].click()
        self.find_elements(*self.Asia_xpath)[1].click()
        time.sleep(2)
        self.find_elements(*self.Bangkok_xpath)[0].click()

    def select_intFlightGoDate(self):
        time.sleep(1)
        self.find_element(*self.oneWay_xpath).click()
        time.sleep(1)
        self.find_element(*self.inputGoDate_xpath).click()
        time.sleep(2)
        print(self.dateIntFlightGoDay_xpath)
        self.find_elements(*self.dateIntFlightGoDay_xpath)[0].click()
        time.sleep(2)

    def click_intFlightyuding(self):
        time.sleep(5)

        self.find_elements(*self.intFlight_yuding)[0].click()
        print(self.intFlight_yuding)
        time.sleep(2)
        self.find_elements(*self.intFlight_yuding_anniu)[0].click()
        time.sleep(2)






    def goOrderList(self):
        time.sleep(2)
        self.smart_click(*self.orderList_xpath)
        time.sleep(5)

    def ignoreRecommend(self):
        time.sleep(2)
        self.smart_click(*self.continueBook_xpath)
        time.sleep(2)



